package com.whf.android.jar.base.latte;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;

import androidx.annotation.Nullable;
import androidx.appcompat.widget.ContentFrameLayout;

import com.blankj.utilcode.util.ActivityUtils;
import com.whf.android.jar.R;
import com.whf.android.jar.app.Latte;
import com.whf.android.jar.base.delegate.BaseDelegate;
import com.whf.android.jar.base.oi.IOutListener;
import com.whf.android.jar.media.MediaManage;
import com.whf.android.jar.util.log.LatteLogger;

import me.yokeyword.fragmentation.ExtraTransaction;
import me.yokeyword.fragmentation.ISupportActivity;
import me.yokeyword.fragmentation.SupportActivityDelegate;
import me.yokeyword.fragmentation.anim.DefaultHorizontalAnimator;
import me.yokeyword.fragmentation.anim.FragmentAnimator;

import qiu.niorgai.StatusBarCompat;

/**
 * 唯一的activity
 *
 * @author 傅令杰
 * @version 2022/4/2
 */
public abstract class BaseProxyActivity extends AbstractActivity implements ISupportActivity,
        IOutListener {


    private final SupportActivityDelegate DELEGATE = new SupportActivityDelegate(this);

    /*** 父控件 */
    protected View mRootView = null;

    /**
     * 本界面展示
     *
     * @return LatteDelegate
     */
    public abstract BaseDelegate setRootDelegate();

    /**
     * 设置设置状态栏字体图标深色(true)或浅色(false)
     */
    protected boolean isDark() {
        return true;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        DELEGATE.onCreate(savedInstanceState);
        initContainer(savedInstanceState);
        //持有当前activity
        Latte.getConfigurator().withActivity(this);
        //父控件
        mRootView = getWindow().getDecorView().findViewById(android.R.id.content);
        //全屏
        StatusBarCompat.translucentStatusBar(this, true);
        //状态栏深色或浅色
        if (isDark()) {
            //SDK >= 23, 将状态栏改为浅色模式(状态栏 icon 和字体会变成深色)
            StatusBarCompat.changeToLightStatusBar(this);
        } else {
            //将状态栏改为深色模式(状态栏 icon 和字体会变成浅色, 即默认模式)
            StatusBarCompat.cancelLightStatusBar(this);
        }
    }

    private void initContainer(@Nullable Bundle savedInstanceState) {
        @SuppressLint("RestrictedApi") final ContentFrameLayout container = new ContentFrameLayout(this);
        container.setId(R.id.delegate_container);
        setContentView(container);
        if (savedInstanceState == null) {
            DELEGATE.loadRootFragment(R.id.delegate_container, setRootDelegate());
        }
    }

    @Override
    protected void onStart() {
        super.onStart();
        if(Latte.isProhibitScreenshot()){
            if (MediaManage.getInstance().checkStoragePermission()) {
                MediaManage.getInstance().init(getContentResolver());
            } else {
                getWindow().addFlags(WindowManager.LayoutParams.FLAG_SECURE);
            }
        }
    }

    @Override
    protected void onStop() {
        super.onStop();
        MediaManage.getInstance().unregister();
    }

    @Override
    protected void onDestroy() {
        DELEGATE.onDestroy();
        super.onDestroy();
        System.gc();
        System.runFinalization();
        MediaManage.getInstance().unregister();
    }

    @Override
    public SupportActivityDelegate getSupportDelegate() {
        return DELEGATE;
    }

    @Override
    public ExtraTransaction extraTransaction() {
        return DELEGATE.extraTransaction();
    }

    @Override
    public FragmentAnimator getFragmentAnimator() {
        return DELEGATE.getFragmentAnimator();
    }

    @Override
    public void setFragmentAnimator(FragmentAnimator fragmentAnimator) {
        DELEGATE.setFragmentAnimator(new DefaultHorizontalAnimator());
    }

    @Override
    public FragmentAnimator onCreateFragmentAnimator() {
        return DELEGATE.onCreateFragmentAnimator();
    }

    @Override
    public void post(Runnable runnable) {

    }

    @Override
    public void onBackPressedSupport() {
        DELEGATE.onBackPressedSupport();
    }

    @Override
    public void onBackPressed() {
        DELEGATE.onBackPressed();
    }

    @Deprecated
    public void setLoading(String loading) {
        showLoading(loading);
    }

    @Deprecated
    public void showLoadingDialog() {
        showLoading();
    }

    @Deprecated
    public void hideLoadingDialog() {
        hideLoading();
    }

    @Override
    public void outSignIn() {
        if (outSignInActivity() != null &&
                !getClass().getName().equals(outSignInActivity().getName())) {
            Intent intent = new Intent();
            intent.setClass(this, outSignInActivity());
            intent.putExtra(STATE_OUT, false);
            ActivityUtils.startActivity(intent);
            finish();
        } else if (outSignInDelegate() != null) {
            getSupportDelegate().start(outSignInDelegate());
        } else {
            LatteLogger.e("未实现退出登录");
        }
    }

    /**
     * 登录界面
     */
    public BaseDelegate outSignInDelegate() {
        return null;
    }
}
